/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import Bean.Err;
import Bean.User;
import exception.LoginException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.LoginManager;

/**
 *
 * @author Vincenzo
 */
public class Login extends HttpServlet {

    private LoginManager lm;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        lm = (LoginManager) getServletContext().getAttribute("login-manager");
        Err e= new Err();
        String addr="";
        try{
        String username = request.getParameter("username");
        String password = request.getParameter("password");
         User w=null;
            w = lm.getWorker(username, password);
            HttpSession session = request.getSession(true) ;
            session.setAttribute("worker", w);
            addr="/jsp/welcome.jsp";
        }
        catch(NullPointerException n)
        {
            addr="/jsp/Error.jsp";
            e.setError(n.toString());

        }
        catch (LoginException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            e.setError(ex.toString());
            addr="/jsp/Error.jsp";
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher(addr);
            dispatcher.forward(request, response);

    } 

 

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.doGet(request, response);
    }



}
